package com.baijiaoxi.common.model.base.service.impl;

import com.baijiaoxi.common.model.base.mapper.CoreMapper;
import com.baijiaoxi.common.model.base.model.CoreModel;
import com.baijiaoxi.common.model.base.service.CoreService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;

public abstract class AbstractCoreService<E extends CoreModel> implements CoreService<E> {

    @Autowired
    @Getter
    protected CoreMapper<E> mapper;
    protected boolean interceptDelBatchAsSingle;

    public AbstractCoreService() {
        try {
            Method beforeDel = this.getClass().getDeclaredMethod("beforeDel", CoreModel.class);
            interceptDelBatchAsSingle = beforeDel != null;
        } catch (Exception e) {
            try {
                Method afterDel = this.getClass().getDeclaredMethod("afterDel", CoreModel.class);
                interceptDelBatchAsSingle = afterDel != null;
            } catch (Exception e1) {
                try {
                    Method afterChange = this.getClass().getDeclaredMethod("afterChange", ChangeType.class, CoreModel.class);
                    interceptDelBatchAsSingle = afterChange != null;
                } catch (Exception ex) {
                    interceptDelBatchAsSingle = false;
                }
            }
        }
    }

    /**
     * 是否像拦截单独删除一样拦截批量删除
     *
     * @return
     */
    public boolean isInterceptDelBatchAsSingle() {
        return interceptDelBatchAsSingle;
    }
}
